let util = {};
// 设置平台的 title
util.title = function (title) {
    title = title || '中联公众号后台系统';
    window.document.title = title;
};

util.inOf = function (arr, targetArr) {
    let res = true;
    arr.map(item => {
        if (targetArr.indexOf(item) < 0) {
            res = false;
        }
    });
    return res;
};

util.oneOf = function (ele, targetArr) {
    if (targetArr.indexOf(ele) >= 0) {
        return true;
    } else {
        return false;
    }
};

util.showThisRoute = function (itAccess, currentAccess) {
    if (typeof itAccess === 'object' && Array.isArray(itAccess)) {
        return util.oneOf(currentAccess, itAccess);
    } else {
        return itAccess === currentAccess;
    }
};

/**
 *  通过路由的name 获取当前路由对象
 * @param routers
 * @param name
 * @returns {*}
 */
util.getRouterObjByName = function (routers, name) {
    if (!name || !routers || !routers.length) {
        return null;
    }
    let routerObj = null;
	let _tempRouter = null;
    for (let item of routers) {
        if (item.name === name) {
			routerObj = item;
			return routerObj;
		} else {
			_tempRouter = util.getRouterObjByName (item.children, name);
			if (_tempRouter) {
				routerObj = _tempRouter;
			}
        }
    }
	return routerObj;
};

/**
 *  title 国际化
 * @param vm
 * @param item
 * @returns {*}
 */
util.handleTitle = function (vm, item) {
    if (!item) {
        return '';
    }
    if (typeof item.title === 'object') {
        return vm.$t(item.title.i18n);
    } else {
        return item.title;
    }
};

/**
 *  判断是否为左侧菜单路由
 * @param routerArray 传递当前路由对象
 * @param name  需要去的路由名称
 */
// util.isOtherRouter = function (routerArray, name) {
//     let isOtherRouter = false;
//     let title = '';
//     routerArray.forEach(child => {
//         if (child.name === name) {
//             // title = util.handleTitle(vm, child);
//             if (routerArray.name === 'otherRouter') {
//                 isOtherRouter = true;
//             }
//         }
//         if (child.children) {
//             let _res = util.isOtherRouter(child.children, name);
//
//         }
//     });
//     return isOtherRouter;
// }

/**
 *  通过路由名称 查找当前路由的路径 用于 Main 的面包屑路径
 * @param vm
 * @param name
 * @returns {Array}
 */
util.setCurrentPath = function (vm, name) {
    let title = '';
    let isOtherRouter = false;
    let currentPathArr = [];
    // 判断是否为左侧菜单
    vm.$store.state.app.routers.forEach(item => {
        if (item.children.length === 1) {
            if (item.children[0].name === name) {
                title = util.handleTitle(vm, item);
                if (item.name === 'otherRouter') {
                    isOtherRouter = true;
                }
            }
        } else {
            item.children.forEach(child => {
                if (child.name === name) {
                    title = util.handleTitle(vm, child);
                    if (item.name === 'otherRouter') {
                        isOtherRouter = true;
                    }
                }
                if (child.children) {
                    child.children.forEach(grandson => {
                        if (grandson.name === name) {
                            title = util.handleTitle(vm, grandson);
                            if (child.name === 'otherRouter') {
                                isOtherRouter = true;
                            }
                        }
                    });
                }
            });
        }
    });
    if (name === 'home-index') {
        currentPathArr = [
            {
                title: util.handleTitle(vm, util.getRouterObjByName(vm.$store.state.app.routers, 'home-index')),
                path: '',
                name: 'home-index'
            }
        ];
    } else if ((name.indexOf('_index') >= 0 || isOtherRouter) && name !== 'home-index') {
        //  顶部路由  isOtherRouter
        let _currentRouter = util.getRouterObjByName(vm.$store.state.app.routers, name);
        currentPathArr = _currentRouter.allPath;
    } else {
        // 筛选当前目录的菜单
        let currentPathObj = vm.$store.state.app.routers.filter(item => {
			if (item.children.length == 0) {
				return item.name === name;
			} else if (item.children.length == 1) {
				return item.children[0].name === name;
			} else {
                let i = 0;
                let childArr = item.children;
                let len = childArr.length;
                while (i < len) {
                    if (childArr[i].name === name) {
                        return true;
                    }
                    if (childArr[i].children) {
                        let n = 0;
                        let grandson = childArr[i].children;
                        let grandsonLen = grandson.length;
                        while (n < grandsonLen) {
                            if (grandson[n].name === name) {
                                return true;
                            }
                            n++;
                        }
                    }
                    i++;
                }
                return false;
            }
        })[0];

		if (currentPathObj.children.length <= 1 && currentPathObj.name === 'home-index') {
            currentPathArr = [
                {
                    title: '首页',
                    path: '',
                    name: 'home-index'
                }
            ];
		} else if (currentPathObj.children.length <= 1 && currentPathObj.name !== 'home-index') {
            currentPathArr = [
                {
                    title: currentPathObj.title,
                    path: '',
                    name: name
                }
            ];
        } else {
            let childObj = currentPathObj.children.filter((child) => {
                if (child.children) {
                    let n = 0;
                    let grandson = child.children;
                    let grandsonLen = grandson.length;
                    while (n < grandsonLen) {
                        if (grandson[n].name === name) {
                            return child;
                        }
                        n++;
                    }
                }
                if (child.name === name) {
                    return child;
                }
            })[0];
            if (childObj.children && childObj.name !== name) {
                currentPathArr = [
                    {
                        title: currentPathObj.title,
                        path: '',
                        name: currentPathObj.name
                    },
                    {
                        title: childObj.title,
                        path: currentPathObj.path + '/' + childObj.path,
                        name: childObj.name
                    },
                    {
                        title: childObj.children[0].title,
                        path: childObj.children[0].path,
                        name: childObj.children[0].name
                    }
                ];
            } else {
                currentPathArr = [
                    {
                        title: currentPathObj.title,
                        path: '',
                        name: currentPathObj.name
                    },
                    {
                        title: childObj.title,
                        path: currentPathObj.path + '/' + childObj.path,
                        name: name
                    }
                ];
            }
        }
    }
    vm.$store.commit('setCurrentPath', currentPathArr);

    return currentPathArr;
};

util.openNewPage = function (vm, name, argu, query) {
    let pageOpenedList = vm.$store.state.app.pageOpenedList;
    let openedPageLen = pageOpenedList.length;
    let i = 0;
    let tagHasOpened = false;
    while (i < openedPageLen) {
        if (name === pageOpenedList[i].name) {  // 页面已经打开
            vm.$store.commit('pageOpenedList', {
                index: i,
                argu: argu,
                query: query
            });
            tagHasOpened = true;
            break;
        }
        i++;
    }
    if (!tagHasOpened) {
        let tag = vm.$store.state.app.tagsList.filter((item) => {
            if (item.children) {
                return name === item.children[0].name;
            } else {
                return name === item.name;
            }
        });
        tag = tag[0];
        if (tag) {
            tag = tag.children ? tag.children[0] : tag;
            if (argu) {
                tag.argu = argu;
            }
            if (query) {
                tag.query = query;
            }
            vm.$store.commit('increateTag', tag);
        }
    }
    vm.$store.commit('setCurrentPageName', name);
};

util.toDefaultPage = function (routers, name, route, next) {
    let len = routers.length;
    let i = 0;
    let notHandle = true;
    while (i < len) {
        if (routers[i].name === name && routers[i].children && routers[i].redirect === undefined) {
            route.replace({
                name: routers[i].children[0].name
            });
            notHandle = false;
            next();
            break;
        }
        i++;
    }
    if (notHandle) {
        next();
    }
};

util.fullscreenEvent = function (vm) {
    vm.$store.commit('initCachepage');
    // 权限菜单过滤相关
    // 全屏相关
};

// util.checkUpdate = function (vm) {
//     axios.get('https://api.github.com/repos/iview/iview-admin/releases/latest').then(res => {
//         let version = res.data.tag_name;
//         vm.$Notice.config({
//             duration: 0
//         });
//         if (semver.lt(packjson.version, version)) {
//             vm.$Notice.info({
//                 title: 'iview-admin更新啦',
//                 desc: '<p>iView-admin更新到了' + version + '了，去看看有哪些变化吧</p><a style="font-size:13px;" href="https://github.com/iview/iview-admin/releases" target="_blank">前往github查看</a>'
//             });
//         }
//     });
// };

export default util;
